package com.hfjyz.expert.expert.bean;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.hfjyz.base.constant.BaseConstant;
import com.hfjyz.base.util.BaseUtil;

public class ExpertSearchBean extends ExpertBean {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = -38037745364528996L;

    private Boolean isOr = false;
    private String birthdayFrom = null;
    private String birthdayTo = null;
    private String sexCodes = null;
    private String topEducationCodes = null;
    private String educationTypeCodes = null;

    private List<Object> paramList = null;

    public Boolean getIsOr() {
        return isOr;
    }

    public void setIsOr(Boolean isOr) {
        this.isOr = isOr;
    }

    public String getBirthdayFrom() {
        return birthdayFrom;
    }

    public void setBirthdayFrom(String birthdayFrom) {
        this.birthdayFrom = birthdayFrom;
    }

    public String getBirthdayTo() {
        return birthdayTo;
    }

    public void setBirthdayTo(String birthdayTo) {
        this.birthdayTo = birthdayTo;
    }

    public List<Object> getParamList() {
        return paramList;
    }

    public String getSexCodes() {
        return sexCodes;
    }

    public void setSexCodes(String sexCodes) {
        this.sexCodes = sexCodes;
    }

    public String getTopEducationCodes() {
        return topEducationCodes;
    }

    public void setTopEducationCodes(String topEducationCodes) {
        this.topEducationCodes = topEducationCodes;
    }

    public String getEducationTypeCodes() {
        return educationTypeCodes;
    }

    public void setEducationTypeCodes(String educationTypeCodes) {
        this.educationTypeCodes = educationTypeCodes;
    }

    public String getWhereClause() {
        String c = null;
        if (isOr) {
            c = " or ";
        } else {
            c = " and ";
        }
        paramList = new ArrayList<Object>();
        StringBuilder sb = new StringBuilder();
        if (!BaseUtil.isEmpty(idNo)) {
            beforeAppend(sb, c);
            sb.append("idNo like ?");
            paramList.add("%" + idNo + "%");
        }

        if (!BaseUtil.isEmpty(name)) {
            beforeAppend(sb, c);
            sb.append("name like ?");
            paramList.add("%" + name + "%");
        }

        if (sex >= 0) {
            beforeAppend(sb, c);
            sb.append("isMale = ?");
            paramList.add(sex == 1);
        }

        if (categoryId != null && categoryId >= 0) {
            beforeAppend(sb, c);
            sb.append("categoryId.id = ?");
            paramList.add(categoryId);
        }

        if (!BaseUtil.isEmpty(sexCodes)) {
            beforeAppend(sb, c);
            String[] sexCodeArray = sexCodes.split(",");
            sb.append(" (");
            for (int i = 0; i < sexCodeArray.length; i++) {
                Integer code = Integer.valueOf(sexCodeArray[i]);
                if (i > 0) {
                    sb.append(" or ");
                }
                sb.append("isMale = ?");
                paramList.add(code == 1);
            }
            sb.append(") ");
        }

        try {
            if (!BaseUtil.isEmpty(birthdayFrom)) {
                beforeAppend(sb, c);
                sb.append("birthday >= ?");
                SimpleDateFormat sdf = new SimpleDateFormat(BaseConstant.DATE_PATTERN_BAR);
                Date dt = sdf.parse(birthdayFrom);
                paramList.add(dt);
            }

            if (!BaseUtil.isEmpty(birthdayTo)) {
                beforeAppend(sb, c);
                sb.append("birthday <= ?");
                SimpleDateFormat sdf = new SimpleDateFormat(BaseConstant.DATE_PATTERN_BAR);
                Date dt = sdf.parse(birthdayTo);
                paramList.add(dt);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        if (!BaseUtil.isEmpty(graduatedFrom)) {
            beforeAppend(sb, c);
            sb.append("graduatedFrom like ?");
            paramList.add("%" + graduatedFrom + "%");
        }

        if (topEducation >= 0) {
            beforeAppend(sb, c);
            sb.append("topEducation >= ?");
            paramList.add(topEducation);
        }

        if (!BaseUtil.isEmpty(topEducationCodes)) {
            beforeAppend(sb, c);
            String[] codeArray = topEducationCodes.split(",");
            sb.append(" (");
            for (int i = 0; i < codeArray.length; i++) {
                Integer code = Integer.valueOf(codeArray[i]);
                if (i > 0) {
                    sb.append(" or ");
                }
                sb.append("topEducation = ?");
                paramList.add(code);
            }
            sb.append(") ");
        }

        if (educationType >= 0) {
            beforeAppend(sb, c);
            sb.append("educationType = ?");
            paramList.add(educationType);
        }

        if (!BaseUtil.isEmpty(educationTypeCodes)) {
            beforeAppend(sb, c);
            String[] codeArray = educationTypeCodes.split(",");
            sb.append(" (");
            for (int i = 0; i < codeArray.length; i++) {
                Integer code = Integer.valueOf(codeArray[i]);
                if (i > 0) {
                    sb.append(" or ");
                }
                sb.append("educationType = ?");
                paramList.add(code);
            }
            sb.append(") ");
        }

        if (!BaseUtil.isEmpty(subject)) {
            beforeAppend(sb, c);
            sb.append("subject like ?");
            paramList.add("%" + subject + "%");
        }

        if (!BaseUtil.isEmpty(workUnit)) {
            beforeAppend(sb, c);
            sb.append("workUnit like ?");
            paramList.add("%" + workUnit + "%");
        }

        if (!BaseUtil.isEmpty(title)) {
            beforeAppend(sb, c);
            sb.append("title like ?");
            paramList.add("%" + title + "%");
        }

        if (!BaseUtil.isEmpty(post)) {
            beforeAppend(sb, c);
            sb.append("post like ?");
            paramList.add("%" + post + "%");
        }

        if (!BaseUtil.isEmpty(skillsCert)) {
            beforeAppend(sb, c);
            sb.append("skillsCert like ?");
            paramList.add("%" + skillsCert + "%");
        }

        if (!BaseUtil.isEmpty(workPhone)) {
            beforeAppend(sb, c);
            sb.append("workPhone like ?");
            paramList.add("%" + workPhone + "%");
        }

        if (!BaseUtil.isEmpty(cellPhone)) {
            beforeAppend(sb, c);
            sb.append("cellPhone like ?");
            paramList.add("%" + cellPhone + "%");
        }

        if (!BaseUtil.isEmpty(imAddress)) {
            beforeAppend(sb, c);
            sb.append("imAddress like ?");
            paramList.add("%" + imAddress + "%");
        }

        if (!BaseUtil.isEmpty(mailAddress)) {
            beforeAppend(sb, c);
            sb.append("mailAddress like ?");
            paramList.add("%" + mailAddress + "%");
        }

        if (!BaseUtil.isEmpty(workAddress)) {
            beforeAppend(sb, c);
            sb.append("workAddress like ?");
            paramList.add("%" + workAddress + "%");
        }

        if (!BaseUtil.isEmpty(address)) {
            beforeAppend(sb, c);
            sb.append("address like ?");
            paramList.add("%" + address + "%");
        }

        if (!BaseUtil.isEmpty(trainExperience)) {
            beforeAppend(sb, c);
            sb.append("trainExperience like ?");
            paramList.add("%" + trainExperience + "%");
        }

        if (!BaseUtil.isEmpty(workExperience)) {
            beforeAppend(sb, c);
            sb.append("workExperience like ?");
            paramList.add("%" + workExperience + "%");
        }

        if (!BaseUtil.isEmpty(judgeExperience)) {
            beforeAppend(sb, c);
            sb.append("judgeExperience like ?");
            paramList.add("%" + judgeExperience + "%");
        }

        if (!BaseUtil.isEmpty(researchAchievement)) {
            beforeAppend(sb, c);
            sb.append("researchAchievement like ?");
            paramList.add("%" + researchAchievement + "%");
        }

        if (!BaseUtil.isEmpty(socialWork)) {
            beforeAppend(sb, c);
            sb.append("socialWork like ?");
            paramList.add("%" + socialWork + "%");
        }

        if (!BaseUtil.isEmpty(remark)) {
            beforeAppend(sb, c);
            sb.append("remark like ?");
            paramList.add("%" + remark + "%");
        }

        return sb.toString();
    }

    private static void beforeAppend(StringBuilder sb, String c) {
        if (sb.length() == 0) {
            sb.append(" where ");
        } else {
            sb.append(c);
        }
    }
}
